import api from '../../../api/index'
import {transmitQuery} from '../../../utils/util'
Component({
  properties: {
    // 是否刷新
    onRefresh: {
      type: Boolean,
      value: false
    }
  },
  lifetimes: {
    attached() {
      this.getArticleData()
    }
  },
  data: {
    pageNo: 0,
    pageSize: 5,
    pageCallback: '',
    moreloading: false,
    finshed: false,
    articleList: []
  },
  // 监听
  observers: {
    'onRefresh'(newVal) {
      if (newVal) {
        this.setData({
          pageNo: 0,
          pageSize: 5,
          pageCallback: '',
          moreloading: false,
          finshed: false,
          articleList: []
        })
        this.getArticleData()
      }
    }
  },
  methods: {
    // 获取资讯列表数据
    getArticleData() {
      if (this.data.finshed) return
      let params = {
        pageEvent: this.data.pageNo,
        pageSize: this.data.pageSize,
        pageCallback: this.data.pageCallback
      }
      // console.log(params)
      this.setData({ moreloading: true })
      api.HSArticleList(params).then((res) => {
        // console.log(res);
        if (`${res.code}` === '1') {
          const articleResp = res.data
          this.setData({
            pageNo: articleResp.page.hasNextPage,
            pageCallback: articleResp.page.pageCallback
          })
          const requestArr = articleResp.articleList
          const oldDataArr = this.data.articleList
          const dataList = oldDataArr.concat(requestArr)
          if (this.data.pageNo === 0) {
            this.setData({ 
              finshed: true,
              moreloading: false
            })
            return
          }
          setTimeout(() => {
            this.setData({ 
              moreloading: false,
              articleList: dataList
            })
          }, 300);
        } 
      })
    },
    // 跳转webview打开外部网页url
    btnClickHandle(e) {
      wx.navigateTo({
        url: `/pages/commonWebview/commonWebview?title=${e.currentTarget.dataset.title}&url=${encodeURIComponent(e.currentTarget.dataset.link)}`
      })
    },
    // 图片预览
    imagePreview(e) {
      const imageList = e.currentTarget.dataset.images
      const index = e.currentTarget.dataset.imgindex
      wx.previewImage({
        current: imageList[index], // 当前显示图片的 http 链接
        urls: imageList // 需要预览的图片 http 链接列表
      })
    }
  }
})